<template>
  <div class="login-content">
    <el-form :model="loginForm" class="form-box" :rules="rules" ref="ruleFormRef">
      <h1 class="form-title">系统登录</h1>
      <el-form-item prop="account">
        <el-input v-model="loginForm.account" placeholder="账号" class="form-style-item">
          <template #prefix>
            <el-icon class="el-input__icon" color="#889aa4" size="22"><UserFilled /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" placeholder="密码" class="form-style-item" autocomplete="no" :type="showPassword?'text':'password'">
          <template #prefix>
            <el-icon class="el-input__icon" color="#889aa4" size="22"><Lock /></el-icon>
          </template>
          <template #suffix>
            <el-icon class="el-input__icon" color="#889aa4" size="22" @click="showPassword = !showPassword">
              <View v-if="showPassword" />
              <Hide v-else />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="verifyCode">
        <div style="display: flex;align-items: center">
          <el-input v-model="loginForm.verifyCode" placeholder="验证码" class="form-style-item"></el-input>
          <img @click="verifyCode" style="margin-left: 30px;cursor: pointer" :src="verifyCodeImg" alt="#">
        </div>
      </el-form-item>
      <el-button type="primary" style="width: 100%;height: 36px;margin-top: 30px" @click="login(ruleFormRef)">登录</el-button>
    </el-form>
  </div>
</template>

<script setup>
//引入'vue3-puzzle-vcode'插件
import { reactive, ref,watch } from 'vue'
import { UserFilled,Lock,Hide,View } from '@element-plus/icons-vue'
import {useRouter} from "vue-router";
import {ElLoading, ElMessage} from 'element-plus';
import {_getMenuList, _getRoleAudit, _login, _verifyCode} from "../http/api.js";

const router = useRouter();
const ruleFormRef = ref()
const rules = reactive({
  account:[
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password:[
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, message: '密码长度不小于8位', trigger: 'blur' },
  ],
  verifyCode:[
    { required: true, message: '请输入验证码', trigger: 'blur' },
  ],
})
const loginForm = reactive({
  account:'',
  password:'',
  verifyCodeKey:'', // 验证码key
  verifyCode: '', // 验证码值
  tenantId:'123'
})
const showPassword = ref(false);

const verifyCodeImg = ref(''); // 图片验证码
// 获取图片验证码
const verifyCode = () => {
  _verifyCode()
      .then(res => {
        console.log(res);
        if(res.data.ok) {
          let item = res.data.data;
          verifyCodeImg.value = item.image;
          loginForm.verifyCodeKey = item.key;
        }
      })
      .catch(err => {
        console.log(err);
      })
}
verifyCode()
// 登录API
const login = (formEl) => {
  formEl.validate((valid) => {
    console.log(valid);
    if(valid) {
      success()
    }
  })
}
// 获取菜单列表
const getMenuList = (loginId) => {
  const loading = ElLoading.service({lock: true, text: 'Loading', background: 'rgba(0, 0, 0, 0.7)'})
  _getMenuList(loginId)
      .then(res => {
        console.log(res);
        loading.close();
        if(res.data.ok) {
          let item = res.data.data;
          console.log(item);
          sessionStorage.setItem('menuList',JSON.stringify(item));
          getRoleAudit(loginId,item[0].children[0].component)
        }
      })
      .catch(err => {
        console.log(err);
        loading.close();
      })
}
// 获取角色审核权限
const getRoleAudit = (id,path) => {
  const loading = ElLoading.service({lock: true, text: 'Loading', background: 'rgba(0, 0, 0, 0.7)'})
  _getRoleAudit(id)
      .then(res => {
        console.log(res);
        loading.close();
        if(res.data.ok) {
          let item = res.data.data;
          let oneFlag = item.oneFlag;
          let twoFlag = item.twoFlag;
          let threeFlag = item.threeFlag;
          let auditName = item.auditName;
          sessionStorage.setItem('oneFlag',oneFlag)
          sessionStorage.setItem('twoFlag',twoFlag)
          sessionStorage.setItem('threeFlag',threeFlag)
          sessionStorage.setItem('auditName',auditName)
          router.replace({ path })
        }
      })
      .catch(err => {
        console.log(err);
        loading.close();
      })
}

//用户通过了验证
const success = () => {
  const loading = ElLoading.service({lock: true, text: 'Loading', background: 'rgba(0, 0, 0, 0.7)' })
  _login(loginForm)
      .then(res => {
        console.log(res);
        loading.close();
        if(res.data.ok) {
          let { tokenName,tokenValue,loginId } = res.data.data;
          sessionStorage.setItem('tokenName',tokenName);
          sessionStorage.setItem('tokenValue',tokenValue);
          sessionStorage.setItem('loginId',loginId);
          sessionStorage.setItem('account',loginForm.account);
          getMenuList(loginId);
        }else {
          verifyCode()
          ElMessage({ message: res.data.msg, type: 'warning'});
        }
      })
      .catch(err => {
        console.log(err);
        loading.close();
        ElMessage({ message: err.data.msg || '网络异常', type: 'error'});
      })
}

</script>

<style scoped lang="scss">
.login-content{
  max-width: 520px;
  margin: 160px auto 0 auto;
  background-color: #ffffff;
  .form-box{
    position: relative;
    max-width: 100%;
    padding:30px;
    margin: 0 auto;
    overflow: hidden;
    .form-title{
      text-align: center;
      margin-bottom: 40px;
    }
    .form-style-item{
      height: 50px;
      .el-input__icon{
        cursor: pointer;
      }
    }
    .form-style-item :deep .el-input__wrapper{
      background: rgba(0,0,0,.1);
      border:1px solid hsla(0,0%,100%,.1);
      box-shadow: none;
      /*.el-input__inner{
        color: #ffffff;
      }*/
    }
  }

}
</style>